package model.impl.network;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.HashMap;

import org.resotc.model.Light;
import org.resotc.model.TrafficLight;

import sim.util.Double2D;

public class TrafficLightImpl implements org.resotc.model.TrafficLight {
	private int x;
	private int y;
	private int trafficLightCount;
	private HashMap<Integer, Light> lights;
	private HashMap<Integer, Double2D> lightsPos;
	public TrafficLightImpl(int trafficLightCount) {

		this.trafficLightCount = trafficLightCount;
		lights = new HashMap<Integer, Light>();
		for (int i=0;i< trafficLightCount;i++) {
			lights.put(i, new LightImpl(Color.red));
		}
	}

	/**
	 * @see TrafficLight#getX()
	 */
	public int getX() {
		return this.x;
	}

	/**
	 * @see TrafficLight#getY()
	 */
	public int getY() {
		return this.y;
	}

	/**
	 * @see TrafficLight#setX()
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * @see TrafficLight#setY()
	 */
	public void setY(int y) {
		this.y = y;
	}

	/**
	 * @see TrafficLight#getLightCount()
	 */
	public int getLightCount() {
		return this.trafficLightCount;
	}

	/**
	 * @see TrafficLight#setLightCount()
	 */
	public void setLightCount(int countLight) {
		this.trafficLightCount = countLight;

	}

	/**
	 * @see TrafficLight#setColor()
	 */
	public void setColor(int lightId, Light light) {
		lights.put(lightId, light);
	}

	/**
	 * @see TrafficLight#getColor()
	 */
	public Color getColor(int lightId) {
		
		if (lights.get(lightId).getColor() == Color.green){
			return Color.green;	
		}
		else {
			
			return Color.red;
		}
		
	}

	/**
	 * @see TrafficLight#calcLightPos(Point2D, Point2D)
	 */
	public HashMap<Integer, Double2D> calcLightPos(Double2D IntersectionPos,
			Double2D StreetPos) {
		HashMap<Integer, Double2D> lightsPos = new HashMap<Integer, Double2D>();
		for (int i = 0; i < this.trafficLightCount; i++) {
			double Xneu = 0;
			double Yneu = 0;
			double delta = 3 / (Math.sqrt((IntersectionPos.getX() - StreetPos
					.getX())
					* (IntersectionPos.getX() - StreetPos.getX())
					+ (IntersectionPos.getY() - StreetPos.getY())
					* (IntersectionPos.getY() - StreetPos.getY())));
			double rx = IntersectionPos.getX()
					+ (delta * (StreetPos.getX() - IntersectionPos.getX()));
			double ry = IntersectionPos.getY()
					+ (delta * (StreetPos.getY() - IntersectionPos.getY()));

			if (StreetPos.getY() - IntersectionPos.getY() == 0
					&& StreetPos.getX() - IntersectionPos.getX() != 0) {
				// x constant
				Xneu = rx;
				if(StreetPos.getX()-IntersectionPos.getX() >0)
					Yneu = ry - i;
				else
					Yneu = ry + i;
			} else if (StreetPos.getX() - IntersectionPos.getX() == 0) {
				// y constant
				if(StreetPos.getY()-IntersectionPos.getY() >0)
					Xneu = rx + i;
				else
					Xneu = rx - i;
				Yneu = ry;
			} else {
				// schiefe street
				double M = (StreetPos.getY() - IntersectionPos.getY())
						/ (StreetPos.getX() - IntersectionPos.getX());
				double M_Vertikal = -1 / M;
				Xneu = (rx + (i / (Math.sqrt(1 + M_Vertikal))));
				Yneu = M_Vertikal * (Xneu - rx) + ry;
			}
			Double2D lightPunkt = new Double2D(Xneu, Yneu);
			lightsPos.put(i, lightPunkt);
		}
		return lightsPos;
	}
	/**
	 * @see TrafficLight#getLightPos()
	 */
	public HashMap<Integer, Double2D> getLightPos() {
		return this.lightsPos;
	}
	/**
	 * @see TrafficLight#setLightPos()
	 */
	public void SetLightPos(HashMap<Integer, Double2D> lightsPos) {
		this.lightsPos = lightsPos;
		
	}
	/**
	 * @see TrafficLight#getLight()
	 */
	public Light getLight(int lightid) {
		return this.lights.get(lightid);
	}
}
